//
//  YMOperateVideoViewController.m
//  YMMedical
//
//  Created by y s on 2023/7/20.
//

#import "YMOperateVideoViewController.h"
#import "YMOperateVideoListCell.h"
#import "YMOperateVideoDetailVC.h"
#import "YMMainDefaultEmptyCell.h"
#import "YMHomeService.h"

@interface YMOperateVideoViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tvMain;

@property (nonatomic, strong) NSMutableArray *arrList;

@property (nonatomic, assign) NSInteger pageNo;

@end

@implementation YMOperateVideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
    [self.tvMain.mj_header beginRefreshing];
}

#pragma mark -- UI
- (void)setUI{
    self.view.backgroundColor = STheme.D9;
    [self showNavTitle:KLanguage(@"operate_video_txt")];
    [self.view addSubview:self.tvMain];
    [self.tvMain mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    WEAK_SELF;
    [SThemeUtil refershHead:self.tvMain block:^{
        [weakSelf loadData:YES];
    }];
    [SThemeUtil refershFooter:self.tvMain block:^{
        [weakSelf loadData:NO];
    }];
}

- (void)loadData:(BOOL)isRefresh{
    if (isRefresh){
        _pageNo = 1;
    }
    WEAK_SELF;
    [YMHomeService requestHomeOperationVideoListData:self.idStr pageNo:_pageNo success:^(NSArray * _Nonnull arrData) {
        [weakSelf.tvMain.mj_header endRefreshing];
        if (isRefresh){
            [weakSelf.arrList removeAllObjects];
            [weakSelf.tvMain.mj_footer endRefreshing];
        }else{
            if (arrData.count > 0){
                [weakSelf.tvMain.mj_footer endRefreshing];
            }else{
                [weakSelf.tvMain.mj_footer endRefreshingWithNoMoreData];
            }
        }
        if (arrData.count > 0){
            [weakSelf.arrList addObjectsFromArray:arrData];
            weakSelf.pageNo++;
        }
        [weakSelf.tvMain reloadData];
    } failure:^(NSString * _Nonnull message) {
        [weakSelf.tvMain.mj_header endRefreshing];
        [weakSelf.tvMain.mj_footer endRefreshing];
        [SHUD showInfo:message];
    }];
}
#pragma mark -- UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.arrList.count > 0){
        return self.arrList.count;
    }else{
        return 1;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.arrList.count > 0){
        YMOperateVideoListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YMOperateVideoListCell" forIndexPath:indexPath];
        if (indexPath.section < self.arrList.count){
            cell.dict = [SDataUtil dictOf:self.arrList[indexPath.section]];
        }
        return cell;
    }else{
        YMMainDefaultEmptyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YMMainDefaultEmptyCell" forIndexPath:indexPath];
        cell.strDes = KLanguage(@"no_temp_txt");
        return cell;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return [[UIView alloc] init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc] init];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.arrList.count > 0){
        return [YMOperateVideoListCell getCellHeight];
    }else{
        return [YMMainDefaultEmptyCell getCellHeight];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return CGFLOAT_MIN;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section < self.arrList.count){
        YMOperateVideoDetailVC *vc = [[YMOperateVideoDetailVC alloc] init];
        vc.dict = [SDataUtil dictOf:self.arrList[indexPath.section]];
        [SJumpUtil pushVC:vc animated:YES];
    }
}
#pragma mark -- setter and getter
- (UITableView *)tvMain{
    if (!_tvMain) {
        _tvMain = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tvMain.backgroundColor = [UIColor clearColor];
        _tvMain.delegate   = self;
        _tvMain.dataSource = self;
        _tvMain.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tvMain.showsVerticalScrollIndicator   = NO;
        _tvMain.showsHorizontalScrollIndicator = NO;
        _tvMain.estimatedRowHeight = 0;
        _tvMain.estimatedSectionHeaderHeight = 0;
        _tvMain.estimatedSectionFooterHeight = 0;
        _tvMain.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        [_tvMain registerClass:[YMOperateVideoListCell class] forCellReuseIdentifier:@"YMOperateVideoListCell"];
        [_tvMain registerClass:[YMMainDefaultEmptyCell class] forCellReuseIdentifier:@"YMMainDefaultEmptyCell"];
        
    }
    return _tvMain;
}
- (NSMutableArray *)arrList{
    if (_arrList == nil){
        _arrList = [[NSMutableArray alloc] init];
    }
    return _arrList;
}

@end
